package com.bcbim.api_web.department.controller;

import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Code;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.operation_maintenance_interface.department.dao.DepartmentDAO;
import com.bcbim.operation_maintenance_interface.department.dto.DepartmentNameCheckDTO;
import com.bcbim.operation_maintenance_interface.department.service.DepartmentService;
import com.bcbim.operation_maintenance_interface.department.vo.DepartmentDetailVO;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceColorTypeDTO;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceUnassignedDTO;
import com.bcbim.operation_maintenance_interface.space.service.SpaceService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: 部门controller
 * @Author xuehao
 * @Date 2022/03/09  16:48
 * @Version V1.0
 **/
@RequestMapping("/department")
@RestController
public class DepartmentController {

    @Reference(protocol = "dubbo", group = "department", version = "1.0.0")
    private DepartmentService departmentServiceImpl;
    @Reference(protocol = "dubbo", group = "space", version = "1.0.0")
    private SpaceService spaceServiceImpl;

    /**
     * 新增部门
     *
     * @param departmentDAO
     * @author xuehao
     * @createDate 2022/3/9 16:55
     * @retuen ReturnMsg
     */
    @PostMapping("insertDepartment")
    public ReturnMsg insertDepartment(DepartmentDAO departmentDAO) {
        try {
            ParamCheck.idCheck(departmentDAO.getBusinessDepartmentId());
            ParamCheck.stringNotEmpty(departmentDAO.getDepartmentName(), 50);
            //主管手输
            ParamCheck.stringCanBeEmpty(departmentDAO.getDepartmentDirector(), 32);
            ParamCheck.stringCanBeEmpty(departmentDAO.getDepartmentColor(), 20);
            ParamCheck.idCheck(departmentDAO.getProjectId());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //判断部门名称是否相同
        DepartmentNameCheckDTO departmentNameCheckDTO = new DepartmentNameCheckDTO();
        departmentNameCheckDTO.setBusinessDepartmentId(departmentDAO.getBusinessDepartmentId())
                .setDepartmentName(departmentDAO.getDepartmentName())
                .setId(null);
        int count = departmentServiceImpl.checkDepartmentName(departmentNameCheckDTO);
        if (count > 0) {
            return ReturnMsg.consFail("部门名称重复");
        }
        //整理参数
        departmentDAO.setId(GuidUtil.get());
        departmentDAO.setDel(Constant.NOT_DEL);
        departmentDAO.setCreateBy(AuthUtil.getUserIdDecrypt());
        departmentDAO.setCreateTime(LocalDateTime.now());
        int result = departmentServiceImpl.insertDepartment(departmentDAO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 编辑部门
     *
     * @param departmentDAO
     * @author xuehao
     * @createDate 2022/3/9 17:00
     * @retuen ReturnMsg
     */
    @PostMapping("/updateDepartment")
    public ReturnMsg updateDepartment(DepartmentDAO departmentDAO) {
        try {
            ParamCheck.idCheck(departmentDAO.getId());
            ParamCheck.idCheck(departmentDAO.getBusinessDepartmentId());
            ParamCheck.stringNotEmpty(departmentDAO.getDepartmentName(), 50);
            //主管手输
            ParamCheck.stringCanBeEmpty(departmentDAO.getDepartmentDirector(), 32);
            ParamCheck.stringCanBeEmpty(departmentDAO.getDepartmentColor(), 20);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //判断部门名称是否相同
        DepartmentNameCheckDTO departmentNameCheckDTO = new DepartmentNameCheckDTO();
        departmentNameCheckDTO.setBusinessDepartmentId(departmentDAO.getBusinessDepartmentId())
                .setDepartmentName(departmentDAO.getDepartmentName())
                .setId(departmentDAO.getId());
        int count = departmentServiceImpl.checkDepartmentName(departmentNameCheckDTO);
        if (count > 0) {
            return ReturnMsg.consFail("部门名称重复");
        }
        departmentDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
        departmentDAO.setUpdateTime(LocalDateTime.now());
        int result = departmentServiceImpl.updateDepartment(departmentDAO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 删除部门
     *
     * @param id
     * @author xuehao
     * @createDate 2022/3/9 17:03
     * @retuen ReturnMsg
     */
    @PostMapping("/deleteDepartment")
    public ReturnMsg deleteDepartment(String id, String projectId) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //查询部门详情，修改未指派使用
        DepartmentDetailVO department = departmentServiceImpl.getDepartment(id);

        DepartmentDAO departmentDAO = new DepartmentDAO();
        departmentDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
        departmentDAO.setUpdateTime(LocalDateTime.now());
        departmentDAO.setDel(Constant.DEL);
        departmentDAO.setId(id);
        int result = departmentServiceImpl.updateDepartment(departmentDAO);
        //清空空间业务部门指派
        //获取业务部门的未指派id
        String unassignedId = departmentServiceImpl.getUnassignedId(department.getBusinessDepartmentId(), Constant.UNASSIGNED);
        if (StringUtils.isEmpty(unassignedId)) {
            //如果为空，生成部门未指派数据
            DepartmentDAO unassignedDepartment = new DepartmentDAO();
            unassignedId = GuidUtil.get();
            unassignedDepartment.setId(unassignedId);
            unassignedDepartment.setProjectId(projectId);
            unassignedDepartment.setBusinessDepartmentId(department.getBusinessDepartmentId());
            unassignedDepartment.setDepartmentName(Constant.UNASSIGNED);
            unassignedDepartment.setCreateBy(AuthUtil.getUserIdDecrypt());
            unassignedDepartment.setCreateTime(LocalDateTime.now());
            unassignedDepartment.setDel(Constant.NOT_DEL);
            departmentServiceImpl.insertDepartment(unassignedDepartment);
        }
        //整理参数
        SpaceUnassignedDTO unassignedDTO = new SpaceUnassignedDTO();
        unassignedDTO.setDepartmentId(id)
                .setUnassignedId(unassignedId);
        //将空间变为部门未指派
        spaceServiceImpl.updateDepartment(unassignedDTO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.DEL_SUCCESS) : ReturnMsg.consFail(Constant.DEL_FAIL);
    }

    /**
     * 查询部门详情
     *
     * @param id
     * @author xuehao
     * @createDate 2022/3/9 17:04
     * @retuen ReturnMsg
     */
    @PostMapping("/getDepartment")
    public ReturnMsg getDepartment(String id) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        DepartmentDetailVO departmentDetailVO = departmentServiceImpl.getDepartment(id);
        return ReturnMsg.consSuccess(departmentDetailVO);
    }

    /**
     * 业务部下拉
     *
     * @author xuehao
     * @createDate 2022/3/9 17:06
     * @retuen ReturnMsg
     */
    @PostMapping("/pullBusinessDepartment")
    public ReturnMsg pullBusinessDepartment(String projectId) {
        List<Code> list = departmentServiceImpl.pullBusinessDepartment(projectId);
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 颜色下拉
     *
     * @author xuehao
     * @createDate 2022/3/13 15:15
     * @retuen ReturnMsg
     */
    @PostMapping("pullColour")
    public ReturnMsg pullColour() {
        List<Code> list = departmentServiceImpl.pullColour();
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 颜色图例
     *
     * @param spaceColorTypeDTO
     * @author xuehao
     * @createDate 2022/3/13 15:57
     * @retuen ReturnMsg
     */
    @PostMapping("/selectColourCutLine")
    public ReturnMsg selectColourCutline(SpaceColorTypeDTO spaceColorTypeDTO) {
        try {
            ParamCheck.stringCanBeEmpty(spaceColorTypeDTO.getCode(), 1);
            ParamCheck.idCanBeEmpty(spaceColorTypeDTO.getFloorId());
            ParamCheck.idCanBeEmpty(spaceColorTypeDTO.getProjectId());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        List<Code> list = departmentServiceImpl.selectColourCutLine(spaceColorTypeDTO);
        return ReturnMsg.consSuccess(list);
    }
}
